$(document).ready(function() {

	var currentUrl = window.location.pathname;
	var currentUrl = currentUrl.split("/");
	var blogID = currentUrl[3];
	
	var offset = 0;
	var count = 0;
	var toAppend;
	
	var commentID = 0;
	var num = 0;
	var bool = false;
	
	AJAXcomments();
	
	function AJAXcomments() {
		$.ajax({
			type : 'post',
			url : '/comments/getCommentsAJAX/' + blogID + '/' + offset,
			dataType : 'json',
			success : function(json) {
				
				//using a separate query to find COUNT, was using jQuery.isEmptyObject(json.comments)
				//for if elseif conditions before that
				
				count = json.count;

				if( json.count == 0 ) {
					toAppend = '<div>There are no comments</div>';
					appendComments("#comments",toAppend);
				}
					
				else {
					$.each(json.comments, function(values, value) {
						toAppend = '<div class="comments" id="comment_' + value.Comment.id + '"><img class="commentPic" src="http://localhost/img/profile/user/' +
								value.Comment.user_id + '.jpg" width="75" height="75"/><p class="comment">' + 
								value.Comment.comment + '</p><p class="times">' + value.Comment.created + 
								'</p><img class="optionsPic" src="http://localhost/img/options_icon.jpg" width="20" height="20"/></div>';
						/*
						 * ==likesORnot
						 *
						likesORnot(value.Comment.id);
						alert('boolsausage = ' + boolsausage);
						if ( ! boolsausage ) {
						 	toAppend += '<a class="like">Like</a>';
						}
						else {
							toAppend += '<a class="unlike">Unlike</a>';
						}
						*/
						
						/*
						 * ==likesCount
						 *
					 	likesCount(value.Comment.id);
					 	if (num == 1)
					 		toAppend += '<span class="likeCount">1 person likes this.</span>';
					 	else if (num > 1)
					 		toAppend += '<span class="likeCount">' + num + ' people like this.</span>';
						*/
						//num = 0;
						//toAppend += '</div>';
						
						appendComments("#comments",toAppend);
						
						likesORnot(value.Comment.id);
						likesCount(value.Comment.id);
					});
					//end $.each
				}
				
				commentNavigation();
			},
			error : function() {
				alert('Oops!');
			}
		});
		//end ajax call
		
	} //end AJAXcomments function
	
	function like(comment_id) {
		var url = '/likes/like/' + comment_id;
		$.getJSON(url, function(data){});
	}
	
	function unlike(comment_id) {
		var url = '/likes/unlike/' + comment_id;
		$.getJSON(url, function(data){});
	}
	
	function likesORnot(comment_id) {
		var url = '/likes/likesORnot/' + comment_id;
		var append = "";
		$.getJSON(url, function(data) {
			
			if (data.likes.Like.like)
				append += '<a class="like">Like</a>';
			if (!data.likes)
				append += '<a class="unlike">Unlike</a>';
			
			appendComments("#comment_" + comment_id, append);
		});

	}
	
	function likesCount(comment_id) {
		var url = '/likes/likesCount/' + comment_id;
		$.getJSON(url, function(data) {
			var append = ""; 
			if (data.likeCount == 1)
				append += '<span class="likeCount"> 1 person likes this.</span>';
		 	else if (data.likeCount > 1)
				append += '<span class="likeCount"> ' + data.likeCount + ' people like this.</span>';
			
			appendComments("#comment_" + comment_id, append);
		});

	}

	function appendComments(elementString, toAppend) {
		$(elementString).append(toAppend);
	}
	
	$(".like").live('click', function() {
		$id = $(this).parent().attr('id').split("_");	// id should be: comment_x	where x = comment_id int
		like($id[1]);
		
		$(this).empty();
		$(this).removeClass();
		$(this).addClass('unlike');
		$(this).html('Unlike');
	});
	
	$(".unlike").live('click',function() {
		$id = $(this).parent().attr('id').split("_");	// id should be: comment_x	where x = comment_id int
		like($id[1]);
		
		$(this).empty();
		$(this).removeClass();
		$(this).addClass('like');
		$(this).html('Like');
	});

	$(".prev").click(function() {
		$("#comments div.comments").remove();
		offset -= 2;
		AJAXcomments();
	});
	
	$(".next").click(function() {
		$("#comments div.comments").remove();
		offset += 2;
		AJAXcomments();
	});
	
	function commentNavigation() {
		if (count <= offset + 2){
			$(".next").hide();
			$(".prev").hide();
		}	
		if (count > offset + 2) 
			$(".next").show();
		else
			$(".next").hide();
		
		if (offset >= 2) 
			$(".prev").show();	
		else
			$(".prev").hide();
	}
	
	
	

	
});  //end document ready
